﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using FrbaBus.Properties;

namespace FrbaBus.RegistrarLLegadaMicro
{
    public partial class FormRegistroLlegada : Form
    {
        public FormRegistroLlegada()
        {
            InitializeComponent();
            CargarCombos();
        }

        private void CargarCombos()
        {
            string query = "";

            query = "select distinct ciu_nombre from THE_BEST_IAS.ciudades order by 1";

            SqlDataReader dr = DbHelper.ExecuteQuery(query);

            if (dr.HasRows)
            {
                int i = 0;
                while (dr.Read())
                {
                    cbOrigen.Items.Insert(i, dr[0].ToString());
                    cbDestino.Items.Insert(i, dr[0].ToString());

                    i++;
                }
            }
        }

        private void btnRegistrar_Click(object sender, EventArgs e)
        {
            string sConnectionString = Settings.Default.GD1C2013ConnectionString;
            SqlConnection connection = new SqlConnection(sConnectionString);
            SqlCommand cmd = new SqlCommand("THE_BEST_IAS.pr_registrar_llegada", connection);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@p_fecha_llegada", SqlDbType.DateTime)).Value = Convert.ToDateTime(dtpLlegada.Text.ToString().Trim());
            cmd.Parameters.Add(new SqlParameter("@p_micro_patente", SqlDbType.VarChar)).Value = tbPatente.Text.Trim().ToString();
            cmd.Parameters.Add(new SqlParameter("@p_ciu_origen", SqlDbType.VarChar)).Value = cbOrigen.SelectedItem.ToString();
            cmd.Parameters.Add(new SqlParameter("@p_ciu_destino", SqlDbType.VarChar)).Value = cbDestino.SelectedItem.ToString();

            cmd.Parameters.Add("@returnValue", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
            cmd.CommandTimeout = 1000;

            connection.Open();
            cmd.ExecuteNonQuery(); 

            int valorRetorno = Convert.ToInt32(cmd.Parameters["@returnValue"].Value);

            connection.Close();

            if (valorRetorno == 0)
            {
                MessageBox.Show("Se registro correctamente la llegada del micro", "INFORMACION");
            }
            if (valorRetorno == 1)
            {
                MessageBox.Show("No se registro correctamente la llegada del micro", "ERROR");
            }
            if (valorRetorno == 2)
            {
                MessageBox.Show("No existe viaje para la combinacion fecha/micro/origen/destino elegidos", "ERROR");
            }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}